﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace Omnia.UX
{
    public class SelectListBoxItem : TriggerAction<DependencyObject>
    {
        // Search the visual tree (parents) to find a ListBoxItem
        // and select the item on Mouse Enter.
        protected override void Invoke(object parameter)
        {
            DependencyObject parent = this.AssociatedObject as DependencyObject;
            while (parent != null)
            {
                // If a ListBoxItem is found, select it.
                if (parent.GetType() == typeof(ListBoxItem))
                {
                    ListBoxItem listBoxItem = parent as ListBoxItem;
                    listBoxItem.IsSelected = true;
                    break;
                }
                parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
            }
        }
    }
}
